import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grab_the_order/app/data/services/permission_service.dart';
import 'package:grab_the_order/app/modules/home/home_view.dart';
import 'package:grab_the_order/app/modules/profile/profile_view.dart';
import 'package:grab_the_order/app/modules/rule/rule_view.dart';

class FrameworkController extends GetxController {
  final permissionService = Get.find<PermissionService>();
  final RxInt currentIndex = 0.obs;

  final List<Widget> pages = [
    const HomeView(),
    const RuleView(),
    const ProfileView(),
  ];
  
  final List<BottomNavigationBarItem> bottomNavItems = const [
    BottomNavigationBarItem(
        icon: Icon(Icons.home_outlined), activeIcon: Icon(Icons.home), label: '首页'),
    BottomNavigationBarItem(
        icon: Icon(Icons.rule_folder_outlined), activeIcon: Icon(Icons.rule_folder), label: '规则'),
    BottomNavigationBarItem(
        icon: Icon(Icons.person_outline), activeIcon: Icon(Icons.person), label: '我的'),
  ];

  @override
  void onReady() {
    super.onReady();
    _autoRequestPermissions();
  }

  Future<void> _autoRequestPermissions() async {
    await Future.delayed(const Duration(milliseconds: 500));
    
    // 【已修改】调用新的核心权限请求方法
    bool corePermissionsGranted = await permissionService.checkAndRequestCorePermissions();
    
    if (corePermissionsGranted) {
      // 核心权限通过后，才引导无障碍
      await permissionService.requestAccessibilityPermission();
    }
  }

  void changePage(int index) {
    currentIndex.value = index;
  }
}